package wty.Bee;

import org.teasoft.bee.osql.*;
import org.teasoft.bee.osql.api.Condition;
import org.teasoft.bee.osql.api.PreparedSql;
import org.teasoft.bee.osql.api.Suid;
import org.teasoft.bee.osql.api.SuidRich;
import org.teasoft.bee.osql.transaction.Transaction;
import org.teasoft.honey.osql.core.BeeFactory;
import org.teasoft.honey.osql.core.ConditionImpl;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;

public class UserDao {
    // 使用SuidRich替代Suid获取增强功能
    private static final SuidRich suid = BeeFactory.getHoneyFactory().getSuidRich();
    private static final PreparedSql preparedSql = BeeFactory.getHoneyFactory().getPreparedSql();

    // === 1. 基础 CRUD ===

    // 插入用户
    public void insert(User user) {
        suid.insert(user);
    }

    // 根据ID查询用户
    public User selectById(String userId) {
        return suid.selectById(User.class, userId);
    }

    // 根据用户名查询用户
    public User selectByUsername(String username) {
        // 方式1：使用Condition条件构造器（适合复杂条件）
        Condition condition = new ConditionImpl();
        condition.op("username", Op.eq, username);  // 添加条件：username字段等于参数值
        List<User> users = suid.select(new User(), condition);
        return users.isEmpty() ? null : users.get(0); // 返回第一个用户或null
    }

    // 更新用户
    public int update(User user) {
        return suid.update(user);
    }

    // 根据ID删除用户
    public int delete(String userId) {
        return suid.deleteById(User.class, userId);
    }

    // === 2. 复杂查询 ===

    // 条件查询：按性别和省份筛选（保持不变）
    public List<User> selectByCondition(Character gender, String province) {
        Condition condition = new ConditionImpl();
        if (gender != null) {
            condition.op("gender", Op.eq, gender);
        }
        if (province != null) {
            condition.op("province", Op.eq, province);
        }
        return suid.select(new User(), condition);
    }

    public static void main(String[] args) {
        UserDao dao = new UserDao();
        User user = dao.selectByUsername("张三");
        System.out.println(user.getUserid());
    }



}